package com.sip.ruleevaluateadapter;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.sip.dbadapter.dao.domain.ApiDefinition;
import com.sip.dbadapter.dao.domain.ScanTypeRest;
import com.sip.dbadapter.dao.domain.SystemInfo;
import com.sip.dbadapter.dao.interfaces.SystemInfoDao;
import com.sip.dbadapter.dao.utility.DaoLoadFactory;
import com.sip.dbadapter.dao.utility.DaoNameConstant;
import com.sip.ruleevaluateadapter.comparator.ApiDefinitionDomainComparator;
import com.sip.ruleevaluateadapter.comparator.ScanTypeRestDomainComparator;

public class EvaluationManager {

    public void evaluateSystemUpdates(final SystemInfo newSystem) throws Exception {

        final SystemInfoDao systemInfoDao = (SystemInfoDao) DaoLoadFactory.getDao(DaoNameConstant.SYSTEM_DAO);

        //TODO : Retrive dao find by name.
        final List<SystemInfo> systemInfos = systemInfoDao.find();
        
 
        SystemInfo persistSystemInfo = null;
        for (final SystemInfo systemInfo : systemInfos) {
            
            if (systemInfo.getSystemName().equals(newSystem.getSystemName())) {
                persistSystemInfo = systemInfo;
                break;
            }
        }

        /*ApiDefinition update*/
        final Set<ApiDefinition> scannedApiDefinitions = newSystem.getApiDefinitionSet();
       

        final Set<ApiDefinition> originalApiDefinitions = persistSystemInfo.getApiDefinitionSet();

       
     
        final ApiDefinitionDomainComparator apiDefinitioncomparator = new ApiDefinitionDomainComparator(scannedApiDefinitions);
        final Set<ApiDefinition> updatedDefinitions = apiDefinitioncomparator.compareUpdate(originalApiDefinitions);
        


       
        
        
        
        
        
      /*  ScanTypeRest update 
        final Set<ScanTypeRest> scannedScanTypeRests = getScanTypeFromApiDefinitions(scannedApiDefinitions);
        final Set<ScanTypeRest> originalScanTypeRests = getScanTypeFromApiDefinitions(originalApiDefinitions);

        final ScanTypeRestDomainComparator scanTypeRestComparator = new ScanTypeRestDomainComparator(scannedScanTypeRests);
        final Set<ScanTypeRest> updatedScanTypeRests = scanTypeRestComparator.compareUpdate(originalScanTypeRests);
*/
        // evaluateAPIUpdates(itScanned.next(), itLatest.next());

        // updateSystem(latestSystemInfo);

        persistSystemInfo.setApiDefinitionSet(updatedDefinitions);
        systemInfoDao.saveOrUpdate(persistSystemInfo);

    }

    private Set<ScanTypeRest> getScanTypeFromApiDefinitions(final Set<ApiDefinition> scannedApiDefinitions) {

        Set<ScanTypeRest> set = new HashSet<ScanTypeRest>();

        for (final ApiDefinition apiDefinition : scannedApiDefinitions) {
            set.add(apiDefinition.getScanTypeRest());
        }

        return set;
    }



    /*
     * public void updateSystem(SystemInfo t) { dao.update(t); }
     */
}
